package com.yongbao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yongbao.model.domain.DishOrderDO;
import com.yongbao.model.request.DishOrderRequest;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zhanghong
 */
@Repository
public interface DishOrderMapper extends BaseMapper<DishOrderDO> {
    DishOrderDO selectDOById(Long id);
    List<DishOrderDO> selectDOList(DishOrderDO dishOrder);
    int insertDO(DishOrderDO dishOrder);
    int batchInsertDO(List<DishOrderDO> dishOrderDOList);
    int updateDO(DishOrderDO dishOrder);
    int deleteDOById(Long id);
    @Select({"<script>",
            "   select count(1) from dish_order",
            "   <where>",
            "       deleted=0",
            "       <if test=\"param.orderType!=null\">and order_type=#{param.orderType}</if>",
            "       <if test=\"param.bookDinner!=null\">and book_dinner=#{param.bookDinner}</if>",
            "       <if test=\"param.bookLunch!=null\">and book_lunch=#{param.bookLunch}</if>",
            "       <if test=\"param.bookBreakfast!=null\">and book_breakfast=#{param.bookBreakfast}</if>",
            "       <if test=\"param.orderType!=null\">and order_type=#{param.orderType}</if>",
            "       <if test=\"param.startCreateTime!=null\">and create_time &gt; #{param.startCreateTime}</if>",
            "       <if test=\"param.endCreateTime!=null\">and create_time &lt; #{param.endCreateTime}</if>",
            "       <if test=\"param.areaName!=null and param.areaName!=''\">and area_name = #{param.areaName}</if>",
            "       <if test=\"param.keyWord!=null and param.keyWord!=''\">and (area_name like concat('%',#{param.keyWord},'%') or street_name like concat('%',#{param.keyWord},'%') or address like concat('%',#{param.keyWord},'%'))</if>",
            "   </where>",
            "</script>"})
    long pageCount(@Param("param") DishOrderRequest param);

    int deleteDOByIds(String[] ids);
    @Select({"<script>",
            "   select * from dish_order",
            "   <where>",
            "       deleted=0",
            "       <if test=\"param.orderType!=null\">and order_type=#{param.orderType}</if>",
            "       <if test=\"param.bookDinner!=null\">and book_dinner=#{param.bookDinner}</if>",
            "       <if test=\"param.bookLunch!=null\">and book_lunch=#{param.bookLunch}</if>",
            "       <if test=\"param.bookBreakfast!=null\">and book_breakfast=#{param.bookBreakfast}</if>",
            "       <if test=\"param.startCreateTime!=null\">and create_time &gt; #{param.startCreateTime}</if>",
            "       <if test=\"param.endCreateTime!=null\">and create_time &lt; #{param.endCreateTime}</if>",
            "       <if test=\"param.areaName!=null and param.areaName!=''\">and area_name = #{param.areaName}</if>",
            "       <if test=\"param.keyWord!=null and param.keyWord!=''\">and (area_name like concat('%',#{param.keyWord},'%') or street_name like concat('%',#{param.keyWord},'%') or address like concat('%',#{param.keyWord},'%'))</if>",
            "   </where>",
            "   order by create_time desc limit #{limit} offset #{offset}",
            "</script>"})
    List<DishOrderDO> pageQuery(@Param("param") DishOrderRequest param, @Param("limit") int limit, @Param("offset") long offset);

    DishOrderDO queryRecentCreatUserId(Long createUser);

    DishOrderDO queryRecentJoinByUserId(Long userId);

    @Select({"<script>",
            "   select * from dish_order",
            "   <where>",
            "       deleted=0",
            "       <if test=\"param.orderType!=null\">and order_type=#{param.orderType}</if>",
            "       <if test=\"param.bookDinner!=null\">and book_dinner=#{param.bookDinner}</if>",
            "       <if test=\"param.bookLunch!=null\">and book_lunch=#{param.bookLunch}</if>",
            "       <if test=\"param.bookBreakfast!=null\">and book_breakfast=#{param.bookBreakfast}</if>",
            "       <if test=\"param.startCreateTime!=null\">and create_time &gt; #{param.startCreateTime}</if>",
            "       <if test=\"param.endCreateTime!=null\">and create_time &lt; #{param.endCreateTime}</if>",
            "       <if test=\"param.keyWord!=null and param.keyWord!=''\">and (area_name like concat('',#{param.keyWord},'') or street_name like concat('',#{param.keyWord},'') or address like concat('',#{param.keyWord},''))</if>",
            "       <if test=\"param.expiredWeekOrMonthDish != null and param.expiredWeekOrMonthDish==1\">and (order_type in (1, 2) and end_date &lt; now())</if>",
            "   </where>",
            "   order by create_time desc",
            "</script>"})
    List<DishOrderDO> listQuery(@Param("param") DishOrderRequest param);

}
